package com.example.score.account;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fsyj
 */
@Slf4j
@Data
@EnableConfigurationProperties(Manager.class)
@ConfigurationProperties(prefix = "manage", ignoreInvalidFields = false)
public class Manager {
    private List<Account> accounts;

    private List<Referee> referees;

    @PostConstruct
    public void initAccount() {
        if (referees != null) {
            accounts = new ArrayList<>(16);
            for (Referee referee : referees) {
                accounts.add(referee);
                accounts.addAll(referee.getJudges());
            }
        }
    }



    public Account verify(String username, String password) {
        if (accounts == null) {
            initAccount();
        }
        Account verifyAccount = null;
        for (Account account : accounts) {
            if (account.verify(username, password)) {
                verifyAccount = account;
            }
        }
        return verifyAccount;
    }

    public boolean verifyRefereeId(String refereeId) {
        if (refereeId == null || "".equals(refereeId)) {
            return false;
        }
        for (Referee referee : referees) {
            if (referee.getId().equals(refereeId)) {
                return true;
            }
        }
        return false;
    }

    public Referee getRefereeById(String refereeId) {
        if (refereeId == null || "".equals(refereeId)) {
            return null;
        }
        for (Referee referee : referees) {
            if (referee.getId().equals(refereeId)) {
                return referee;
            }
        }
        return null;
    }

    public boolean verifyJudgeId(String judgeId) {
        if (judgeId == null || "".equals(judgeId)) {
            return false;
        }
        try {
            for (Account account : accounts) {
                if (account.getId().equals(judgeId)) {
                    return true;
                }
            }
        } catch (NullPointerException e) {
            log.error("id 为空");
        }
        return false;
    }

    public Account getJudgeById(String judgeId) {
        if (judgeId == null || "".equals(judgeId)) {
            return null;
        }
        for (Account account : accounts) {
            if (account.getId().equals(judgeId)) {
                return account;
            }
        }
        return null;
    }

    /**
     * 通过裁判账号获取裁判长账号
     * @param account
     * @return
     */
    public Account getRefereeByAccount(Account account) {
        for (Referee referee : referees) {
            List<Account> judges = referee.getJudges();
            for (Account judge : judges) {
                if (judge.getId().equals(account.getId())) {
                    return referee;
                }
            }
        }
        return null;
    }
}
